<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>docker | 学习笔记</title><meta name="author" content="lzoxun"><meta name="copyright" content="lzoxun"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="概述 解决问题:  部署环境麻烦，相同环境更麻烦  项目与环境一同打包（镜像），发布到docker仓库  &#x3D;&gt; 用户(运维)下载直接运行  隔离思想Docker核心思想 打包装箱 每个箱子相互隔离互不影响  基本信息基于go开发的容器技术、虚拟化技术(系统最核心的环境 + mysql + 需要的技术2 + 需要的技术n ..) 打包成镜像 ，多个再相同系统运行   直接运行在宿主">
<meta property="og:type" content="article">
<meta property="og:title" content="docker">
<meta property="og:url" content="http://example.com/2023/07/22/other/othier-docker/index.html">
<meta property="og:site_name" content="学习笔记">
<meta property="og:description" content="概述 解决问题:  部署环境麻烦，相同环境更麻烦  项目与环境一同打包（镜像），发布到docker仓库  &#x3D;&gt; 用户(运维)下载直接运行  隔离思想Docker核心思想 打包装箱 每个箱子相互隔离互不影响  基本信息基于go开发的容器技术、虚拟化技术(系统最核心的环境 + mysql + 需要的技术2 + 需要的技术n ..) 打包成镜像 ，多个再相同系统运行   直接运行在宿主">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png">
<meta property="article:published_time" content="2023-07-22T07:14:09.685Z">
<meta property="article:modified_time" content="2023-07-22T07:14:09.685Z">
<meta property="article:author" content="lzoxun">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2023/07/22/other/othier-docker/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":true,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'docker',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-07-22 15:14:09'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">108</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="学习笔记"><span class="site-name">学习笔记</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">docker</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-22T07:14:09.685Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-07-22T07:14:09.685Z" title="更新于 2023-07-22 15:14:09">2023-07-22</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="docker"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><img src= "" data-lazy-src="D:\MyData\projects\lzo-docs-blog-2\static\img\dockerall.png"></p>
<h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><ul>
<li><p>解决问题:<br>  部署环境麻烦，相同环境更麻烦<br>  项目与环境一同打包（镜像），发布到<code>docker仓库</code>  &#x3D;&gt; 用户(运维)下载直接运行</p>
</li>
<li><p>隔离思想<br>Docker核心思想 打包装箱 每个箱子相互隔离互不影响</p>
</li>
<li><p>基本信息<br>基于go开发的<br>容器技术、虚拟化技术(系统最核心的环境 + mysql + 需要的技术2 + 需要的技术n ..) 打包成镜像 ，多个再相同系统运行<br>   <code>直接运行在宿主机上，容器没有自己内核，也没有不必要的软件</code><br>   <code>每个容器相互隔离，有自己的文件系统，互不影响</code><br>   <code>内核级别的虚拟化</code><br>虚拟机 &#x3D;&gt; 虚拟机资源占用、冗余步骤多、启动速度慢、很多东西都不是程序环境所需要的<br>   <code>运行一个完整的系统，在系统上安装和运行软件</code><br>   <code>如果需要连个容器，就需要安装连个centos这样的系统</code><br>   <code>安全性强</code><br>基本组成架构</p>
</li>
<li><p>DevOpts(开发、运维)<br>打包镜像，发布运维，一键运行，运行环境高度一致</p>
</li>
<li><p>名词<br>镜像(image):<br>类似模板，可以通过这个模板创建容器服务<br>容器(container)<br>利用容器运行一个或一组应用,通过镜像创建<br>可以启动停止删除等基本命令<br>仓库(repository)<br>远程存放镜像的地方</p>
</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://hub.docker.com/">仓库地址</a><br><a target="_blank" rel="noopener" href="https://www.docker.com/">官网</a><br><a target="_blank" rel="noopener" href="https://docs.docker.com/">文档</a><br><a target="_blank" rel="noopener" href="https://docs.docker.com/reference/">指令文档</a></p>
<h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><p>…<br>…<br>…<br><code>/var/lib/docker</code>:工作目录</p>
<p>安装成功后<br><code>systemctl start docker.service</code> 启动服务<br><code>docker run hello-world</code> 测试是否可以用</p>
<ul>
<li>docker 的工作原理<ul>
<li>docker 是一个Client-Server结构系统，Docker守护进程运行在服务器主机子上，通过Socket从客户端访问<ul>
<li>守护进程包含所有容器，相当于小的Linux系统，里面的端口与外界不冲突</li>
</ul>
</li>
<li>Docker服务器 执行 客户端发送的指令</li>
</ul>
</li>
</ul>
<h3 id="命令"><a href="#命令" class="headerlink" title="命令"></a>命令</h3><p>帮助: <code>docker --help</code><br>判断是否安装成功: <code>docker version</code>、<code>docker info</code><br>启动程序: <code>docker run &lt;镜像名称&gt;</code></p>
<ul>
<li>镜像命令<ul>
<li><code>docker images</code>：查看本地镜像<ul>
<li><code>-aq</code>:查出所有镜像id</li>
</ul>
</li>
<li><code>docker search &lt;image name&gt;</code>：搜索镜像<ul>
<li><code>--filter=STARS=3000</code> :只搜索stars大于3000的镜像</li>
</ul>
</li>
<li><code>docker pull &lt;image name&gt;</code>:下载镜像<ul>
<li><code>docker pull &lt;image name&gt;:tag</code> 指定版本</li>
</ul>
</li>
<li><code>docker rmi -f &lt;image id|name&gt;</code>:删除镜像<ul>
<li><code>docker rmi -f $(docker images -aq)</code>:批量删除</li>
</ul>
</li>
</ul>
</li>
</ul>
<blockquote>
<p>下载测试镜像</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull centos</span><br></pre></td></tr></table></figure>
<ul>
<li><p>容器命令</p>
<!-- -   `docker linux大多数命令 容器id`:实现容器内部相应linux功能    -->
<ul>
<li><p><code>docker run [参数] centos</code>:新建容器并启动</p>
<ul>
<li><code>--name Name</code>:容器名称</li>
<li><code>-d</code>:后台运行<ul>
<li><code>后台运行必须有一个前台在使用的应用，否则启动后会自动停止</code></li>
</ul>
</li>
<li><code>-it centos /bin/bash</code>: 进入容器 交互运行,里面是单独的centos系统<ul>
<li><code>exit</code>：停止并推出</li>
<li><code>Ctrl + p + q</code>:不停止推出</li>
<li><code>docker exec -it &lt;id|name&gt; /bin/bash</code> 进入当前正在运行的容器,并开启新终端，可以操作 </li>
<li><code>docker attach &lt;id|name&gt;</code> 进入当前正在运行的容器，正则运行的终端  </li>
<li>&#96;&#96;:重新进入停止的容器</li>
</ul>
</li>
<li><code>--rm</code>:用完就删除</li>
<li><code>-小p IP:8080:8080</code>:指定容器端口，可以多个<ul>
<li>IP:主机端口:容器端口</li>
</ul>
</li>
<li><code>-大P</code>:随机容器端口 <!-- -   `docker update --restart=always <容器 id>` :更新参数 --></li>
</ul>
</li>
<li><p><code>docker ps</code>:查看运行中的容器</p>
<ul>
<li><code>-a</code>:历史运行的容器</li>
<li><code>-n=1</code>:只看一个</li>
<li><code>-q</code>:只显示编号</li>
</ul>
</li>
<li><p><code>docker rm &lt;容器 id&gt;</code>:删除容器  </p>
<ul>
<li><code>docker rm $(docker ps -aq)</code>:删除所有</li>
<li><code>docker rm -a -q|xargs docker rm</code>:通过Linux的xargs批量删除</li>
<li><code>-f</code>：强制删除运行的容器</li>
</ul>
</li>
<li><p><code>docker start|stop|restart|kill &lt;容器 id&gt;</code>:启动停止容器</p>
</li>
<li><p><code>docker pause|unpause &lt;容器id&gt;</code>:暂停容器</p>
</li>
<li><p><code>docker logs &lt;容器 id&gt;</code>:查看日志</p>
<ul>
<li><code>-f -t --tail number</code></li>
</ul>
</li>
<li><p><code>docker inspect &lt;容器 id&gt;</code>:查看容器信息 重要</p>
</li>
<li><p><code>docker top &lt;容器 id&gt;</code>:查看进程信息 </p>
</li>
<li><p><code>docker stats</code>:查看资源占用</p>
</li>
<li><p><code>docker cp &lt;容器 id&gt;:/容器内路径 外部路径</code>:docker文件拷贝</p>
</li>
<li><p><code>docker history &lt;容器 id&gt;</code>：查看容器的构建过程</p>
</li>
<li><p>docker save&#x2F;load&#x2F;import 保存镜像压缩包,加载镜像压缩包</p>
</li>
</ul>
</li>
<li><p>docker run 做了什么</p>
<ul>
<li>在本机寻找镜像，如果有运行镜像</li>
<li>否则去远程仓库找，如果找到下载镜像到本地，在运行</li>
<li>否则返回找不到</li>
</ul>
</li>
</ul>
<h3 id="基本案例"><a href="#基本案例" class="headerlink" title="基本案例"></a>基本案例</h3><ul>
<li><p>安装nginx</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker pull nginx // 下载</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -d --name=nginx1 -p 3344:80 nginx // 开启  容器内nginx启动80端口，暴露出来映射到外部主机的3344</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">curl localhost:3344  // 测试</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>安装 mysql</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">docker pull mysql</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">共享配置文件 数据文件 配置账号密码</span></span><br><span class="line">docker run -d -p 3306:3306 -v /tmp/mysql/conf:/etc/mysql/conf.d -v /tmp/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql --name some-mysql mysql   </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"> --</span></span><br><span class="line">直接关闭问题</span><br><span class="line">http://t.zoukankan.com/whereGo-p-12599723.html</span><br><span class="line"></span><br><span class="line">docker run -it -m 300M --memory-reservation 200M -p 3310:3306 -v /tmp/mysql/conf:/etc/mysql/conf.d -v /tmp/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql --name some-mysql mysql /bin/bash</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>安装 tomcat(默认暴露端口8080)</p>
</li>
<li><p>es+kibana</p>
</li>
</ul>
<h3 id="镜像"><a href="#镜像" class="headerlink" title="镜像"></a>镜像</h3><blockquote>
<p>镜像是一种轻量可执行的独立软件包，用来打包软件运行环境，和基于运行环境开发的软件，它包含运行某个软件所需的所有内容，包括<code>代码</code>、<code>库</code>、<code>运行时</code>、<code>环境变量</code>，<code>配置文件</code>等</p>
</blockquote>
<h4 id="镜像加载原理"><a href="#镜像加载原理" class="headerlink" title="镜像加载原理"></a>镜像加载原理</h4><blockquote>
<p>UnionFS（联合文件系统）</p>
</blockquote>
<p>系统主要包含bootfs(公用，不包含镜像中)、rootfs（Linux的&#x2F;dev,&#x2F;proc等文件目录）和独立的应用配置</p>
<p>是一种分层，轻量级高性能的文件系统(系统存在某个镜像，当拉去的二个镜像时，第一层如果与已有镜像相同，就不需要下载，直接拉取第二层);<br>下载过的不会再下载，拉取时版本号后面提示<code>Already exists</code></p>
<p>Docker镜像都是只读的（镜像层），容器启动时，一个新的写入(用户操作 如:run)会被加载到镜像的顶部(自己的一层容器层)</p>
<blockquote>
<p>commit镜像</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker commit 提交容器</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker commit -m=<span class="string">&quot;描述信息&quot;</span> -a=<span class="string">&quot;作者&quot;</span> 容器<span class="built_in">id</span> 目标镜像名:[TAG]</span></span><br><span class="line"></span><br><span class="line">下载官方tomcat</span><br><span class="line">run -it 进入容器修改内容</span><br><span class="line">正在运行的容器层有一个新的ID、</span><br><span class="line">通过这个id commit 做成镜像`存在本地`，(保存容器状态,相当于虚拟机快照)</span><br><span class="line">发布</span><br><span class="line"></span><br><span class="line">提交的时候上层的id都有存在信息中,pull的时候会先下载</span><br></pre></td></tr></table></figure>
<h3 id="容器数据卷"><a href="#容器数据卷" class="headerlink" title="容器数据卷"></a>容器数据卷</h3><h4 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h4><blockquote>
<p>每次修改容器内配置需要进入很麻烦，我们可以在容器外不提供一个映射路径，达到容器外修改配置文件，容器内自动修改的技术<br>数据不能存在容器里面，<code>容器数据卷</code>可以 类似将容器内的目录挂在到主机上(但是它时同步过来的) 做到容器内外数据共享，容器间也可以</p>
</blockquote>
<h4 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h4><blockquote>
<p> 方式一: 通过命令挂载  -v （volume）</p>
</blockquote>
<p>安全模块selinux把权限禁掉了，无法使用-v命令进行挂载<br>有时-v 容器无法启动 <code>chcon -Rt svirt_sandbox_file_t /dir/x/x</code> 开放权限 重新run<br>-R 递归处理所有的文件及子目录<br>-t 设置指定类型的目标安全环境</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -v 主机目录:容器内目录</span><br><span class="line">docker inspect &lt;容器 id&gt;  =&gt; Mounts 查看是否挂载成功</span><br></pre></td></tr></table></figure>

<h4 id="具名挂载与匿名挂载"><a href="#具名挂载与匿名挂载" class="headerlink" title="具名挂载与匿名挂载"></a>具名挂载与匿名挂载</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">-v juname:/inner/dir  具名挂载</span><br><span class="line">-v /inner/der:rw    匿名挂载  rw可读可写 </span><br><span class="line">-v /out/dir:/inner/dir:ro  指定路径挂载，到指定路径 ro只读</span><br></pre></td></tr></table></figure>

<p><code>docker volume ls</code>:查看卷名,匿名的就是自动生成的数据<br><code>docker volume inspect juname</code>：查看详细位置，相关信息等</p>
<p>多个mysql或多个容器间数据共享</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">这里使用到了下面的 dockerfile的挂载 直接 -v也一样</span></span><br><span class="line">dockre run -it --name parent lzoxun/镜像名称 </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">dockre run -it --name child1 --volumes-from parent</span> </span><br><span class="line">dockre run -it --name child3 --volumes-from parent 镜像2 /bin/bash  #child3 通过是镜像2的容器，并继承镜像1的卷</span><br><span class="line">...</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">父子容器挂载目录下的文件相互共享，parent就是数据卷容器</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">所有用到的容器都停止，数据才会没？？？？？</span></span><br></pre></td></tr></table></figure>
<h3 id="DockerFile"><a href="#DockerFile" class="headerlink" title="DockerFile"></a>DockerFile</h3><p>DockerFile是用构建docker镜像的文件，是一个命令脚本，通过这个脚本生成镜像,<br>步骤</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">创建dockerfile文件，文件名自定义</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">文件中的内容 格式: 大写指令 参数</span> </span><br><span class="line">FROM centos</span><br><span class="line">VOLUME [&quot;volume01&quot;,&quot;volume02&quot;,&quot;/xx/xx/volume03&quot;]  #不用-v, volume01这连个是镜像内的目录，在生成镜像时直接挂载的数据卷目录</span><br><span class="line"></span><br><span class="line">CMD echo &quot;---end---&quot;</span><br><span class="line">CMD /bin/bash</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">volume01 数据卷目录外部一定有一个同步的目录</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">一步一步构建镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">运行</span></span><br><span class="line">docker build -f /xx/xxx/dockerfile-name -t lzoxun/镜像名称:[tag?] .   </span><br><span class="line">dockre run -it lzoxun/镜像名称 /bin/bash </span><br><span class="line">docker push # 发布镜像 dockerhub 阿里云等</span><br></pre></td></tr></table></figure>

<h4 id="DockerFIle构建流程"><a href="#DockerFIle构建流程" class="headerlink" title="DockerFIle构建流程"></a>DockerFIle构建流程</h4><p>基础：</p>
<ul>
<li>每个关键字(指令)都必须时大写字母</li>
<li>执行从上到下顺序执行</li>
<li># 表示注释</li>
<li>每一个指令都会创建一个镜像层提交</li>
</ul>
<blockquote>
<p>  dockerfile是面向开发的，后期发布项目做镜像都要编写dockerfile文件。</p>
</blockquote>
<p>步骤<br>    DockerFile：构建文件，定义了一切步骤<br>    DockerImages：镜像时通过DockerFile生成的，最终发布和运行的产品(正常使用冲这里开始 pull)<br>    Docker容器：镜像运行起来提供服务</p>
<blockquote>
<p>  DockerFile指令</p>
</blockquote>
 <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">FROM        # 基础镜像，从这里开始构建</span><br><span class="line">MAINTAINER  # 指定维护者信息 姓名+邮箱</span><br><span class="line">RUN         # 构建镜像时需要运行的命令</span><br><span class="line">ADD 		# 添加镜像</span><br><span class="line">WORKDIR		# 镜像工作目录，进入生成的容器时默认所在目录</span><br><span class="line">VOLUME		# 挂载目录</span><br><span class="line">EXPOSE		# 暴露端口配置 外面的-p</span><br><span class="line">CMD			# 指定容器启动的时候运行的命令，只有追后也给生效</span><br><span class="line">ENTRYPOINT	# 指定容器启动的时候运行的命令，可以追加命令</span><br><span class="line">ONBUILD		# 但构建一个被基础的 DockerFile 就会运行 onbuild 的指令</span><br><span class="line">COPY		# 类似 ADD 将文件拷贝到镜像中</span><br><span class="line">ENV 		# 构建的时候设置环境变量</span><br></pre></td></tr></table></figure>

<blockquote>
<p>  实战测试</p>
</blockquote>
<p><img src= "" data-lazy-src="/../../static/img/dockerfileflow.png" alt="基本流程"></p>
<blockquote>
<p>  tomcat 镜像案例</p>
</blockquote>
<p>1.Dockerfile</p>
<p><img src= "" data-lazy-src="D:\MyData\projects\lzo-docs-blog-2\static\img\dockerfile-tomcat.png"></p>
<p>2.构建镜像</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -t imagesname . # 使用官方的Dockerfile标准文件名不需要 -f</span><br></pre></td></tr></table></figure>

<p>3.启动(run)</p>
<p>4.发布</p>
<blockquote>
<p>  发布到 dockerhub</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker login -u lzoxun -p xxxxxx</span><br><span class="line">docker login -u lzoxun # 控制台输入密码</span><br><span class="line">docker tag 镜像 镜像:1.0 # 加tag</span><br><span class="line">docker push 镜像名:[tag] # 构造镜像的dockerfile的用户名需要与 dockerhub的用户相同</span><br></pre></td></tr></table></figure>

<blockquote>
<p>  发布到阿里云</p>
</blockquote>
<p>1.登录阿里云,找到镜像服务</p>
<p>2.创建命名空间</p>
<p>3.创建仓库,进入有推送步骤</p>
<h3 id="Docker网络原理"><a href="#Docker网络原理" class="headerlink" title="Docker网络原理"></a>Docker网络原理</h3><h3 id="IDEA真和Docker"><a href="#IDEA真和Docker" class="headerlink" title="IDEA真和Docker"></a>IDEA真和Docker</h3><h3 id="Docker-Compose"><a href="#Docker-Compose" class="headerlink" title="Docker Compose"></a>Docker Compose</h3><h3 id="docker-Swarm"><a href="#docker-Swarm" class="headerlink" title="docker Swarm"></a>docker Swarm</h3><h3 id="CI-CD-jenkins"><a href="#CI-CD-jenkins" class="headerlink" title="CI\CD jenkins"></a>CI\CD jenkins</h3><h3 id="可视化工具"><a href="#可视化工具" class="headerlink" title="可视化工具"></a>可视化工具</h3><p>portainer</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 8088:9000 \</span><br><span class="line">--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动成功后 ip:8088 浏览器打开界面</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">设置密码</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">选择本地直接连接</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>lazydocker</p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://example.com">lzoxun</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/07/22/other/othier-docker/">http://example.com/2023/07/22/other/othier-docker/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">学习笔记</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/07/22/other/other-time-class/" title="课程表"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">课程表</div></div></a></div><div class="next-post pull-right"><a href="/2023/07/22/other/other-vimium/" title="vimium"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">vimium</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">lzoxun</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">108</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/liaozhongxun"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/liaozhongxun" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:869664233@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是我的文档</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A6%82%E8%BF%B0"><span class="toc-number">1.</span> <span class="toc-text">概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85"><span class="toc-number">2.</span> <span class="toc-text">安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%BD%E4%BB%A4"><span class="toc-number">3.</span> <span class="toc-text">命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%A1%88%E4%BE%8B"><span class="toc-number">4.</span> <span class="toc-text">基本案例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%9C%E5%83%8F"><span class="toc-number">5.</span> <span class="toc-text">镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%E5%8A%A0%E8%BD%BD%E5%8E%9F%E7%90%86"><span class="toc-number">5.1.</span> <span class="toc-text">镜像加载原理</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-number">6.</span> <span class="toc-text">容器数据卷</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5"><span class="toc-number">6.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8"><span class="toc-number">6.2.</span> <span class="toc-text">使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B7%E5%90%8D%E6%8C%82%E8%BD%BD%E4%B8%8E%E5%8C%BF%E5%90%8D%E6%8C%82%E8%BD%BD"><span class="toc-number">6.3.</span> <span class="toc-text">具名挂载与匿名挂载</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DockerFile"><span class="toc-number">7.</span> <span class="toc-text">DockerFile</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#DockerFIle%E6%9E%84%E5%BB%BA%E6%B5%81%E7%A8%8B"><span class="toc-number">7.1.</span> <span class="toc-text">DockerFIle构建流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker%E7%BD%91%E7%BB%9C%E5%8E%9F%E7%90%86"><span class="toc-number">8.</span> <span class="toc-text">Docker网络原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IDEA%E7%9C%9F%E5%92%8CDocker"><span class="toc-number">9.</span> <span class="toc-text">IDEA真和Docker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-Compose"><span class="toc-number">10.</span> <span class="toc-text">Docker Compose</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-Swarm"><span class="toc-number">11.</span> <span class="toc-text">docker Swarm</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CI-CD-jenkins"><span class="toc-number">12.</span> <span class="toc-text">CI\CD jenkins</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%AF%E8%A7%86%E5%8C%96%E5%B7%A5%E5%85%B7"><span class="toc-number">13.</span> <span class="toc-text">可视化工具</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-11/" title="win 11 set">win 11 set</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-gitbash/" title="git bash">git bash</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-wsl/" title="wsl">wsl</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-main/" title="window">window</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/web/%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98/" title="兼容问题">兼容问题</a><time datetime="2023-07-22T07:14:09.699Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By lzoxun</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/haruto.model.json"},"display":{"position":"right","width":100,"height":200,"right":300},"mobile":{"show":true}});</script></body></html>